<template>
  <div class="login-container">
    <div class="login">
      <div class="logo">CallMeSoul</div>
      <el-card class="box-card">
        <el-form :model="login.data" :rules="login.rules" ref="loginForm" label-width="100px" >
          <el-form-item label="用户名" prop="userName">
            <el-input v-model="login.data.userName"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="passWord">
            <el-input v-model="login.data.passWord"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('loginForm')">立即创建</el-button>
            <el-button @click="resetForm('loginForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>
  import  Api from "../api/index"
export default {
  data () {
    return {
      login: {
          data:{
              userName:"",
              passWord:""
          },
        rules:{
              userName:[
                {required:true,message:"请输入用户名",trigger:"blur"}
              ],
              passWord:[
                {required:true,message:"请输入密码",trigger:"blur"}
              ]
        }
      }
    }
  },
  methods:{
      submitForm:function (formName) {
        this.$refs[formName].validate(valid=>{
            if(valid){
              Api.login(this.login.data).then((response)=>{
                  if(response.status===200){
                      this.$store.commit("LOGIN_SUCCESS",{token:response.data.token});
                      this.$router.push({path:"/"});

                  }else {
                      this.$message.error(response.data.error);
                  }
              })
            }
        })
      },
      resetForm:function (formName) {

      }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.login-container{
  flex: 1;
  display: flex;
  align-items: center;
  align-content: center;
  text-align: center;
}
  .login{
    margin: 0 auto;
  }
  .login .logo{
    font-size: 45px;
  }
  .login .el-card{
    margin-top: 30px;
  }
</style>
